package com.scap.authen.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import com.scap.authen.entity.UserMapping;

@Service
@Repository("voUserMappingDao")
public class UserMappingDaoImpl extends AbstractDao implements UserMappingDao{

	@SuppressWarnings("unchecked")
	@Override
	public List<Object> getAllUserMapping() {
		Criteria criteria = getSession().createCriteria(UserMapping.class);
		List<Object> voUserMappingsList = (List<Object>)criteria.list();
		shutdownSession();
		return voUserMappingsList;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<UserMapping> getUserMappingByUserId(String vaUserId){
		Criteria criteria = getSession().createCriteria(UserMapping.class);
		criteria.add(Restrictions.eq("vaUserId",vaUserId));
		return criteria.list();

	}
	
	@Override
	public boolean insertUserMapping(List<UserMapping> voUserMapping) {
		boolean result = false;
		
		try {
			for(UserMapping voUserMappingTemp : voUserMapping){
				persist(voUserMappingTemp);
			}
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
			result = false;
		}
		
		return result;
	}

	@Override
	public boolean updateUserMapping(List<UserMapping> voUserMapping) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean deleteUserMapping(UserMapping voUserMapping) {
		boolean result = false;
		
		try {

			Query query = getSession().createQuery("delete from UserMapping"
    				+ " where vaUserId = :vaUserId");
			
			query.setString("vaUserId", voUserMapping.getVaUserId());

			query.executeUpdate();

			result = true;
		} catch (Exception e) {
			e.printStackTrace();
			result = false;
		}
		
		return result;
	}

	



	
	
}
